package com.tepth.appmanageservice.protocol;

import com.tepth.appmanageservice.protocol.bodys.CommonReply;
import com.tepth.appmanageservice.protocol.bodys.aes.AesKey;
import com.tepth.appmanageservice.protocol.bodys.login.ClientLogin;
import com.tepth.appmanageservice.protocol.bodys.ordinaryfile.OrdinaryFileRequest;
import com.tepth.appmanageservice.protocol.bodys.ordinaryfile.ViewOrdinaryFileListReply;
import com.tepth.appmanageservice.protocol.bodys.sd.ViewSdSizeReply;
import com.tepth.appmanageservice.protocol.bodys.sd.ViewSdStatusReply;
import com.tepth.appmanageservice.protocol.bodys.sd.ViewSdUsedSizeReply;
import com.tepth.appmanageservice.protocol.bodys.system.PublicKeyRequest;
import com.tepth.appmanageservice.protocol.bodys.system.ViewDomainPortReply;
import com.tepth.appmanageservice.protocol.bodys.system.ViewHeartbeatFrequencyReply;
import com.tepth.appmanageservice.protocol.bodys.system.ViewSystemVersionNumberReply;
import com.tepth.appmanageservice.protocol.interfaces.IProtocolEvent;
import com.tepth.appmanageservice.protocol.packets.Check;
import com.tepth.appmanageservice.protocol.packets.Flag;
import com.tepth.appmanageservice.protocol.packets.Header;
import com.tepth.appmanageservice.protocol.packets.Packets;
import com.tepth.appmanageservice.tools.ext.ExtPro;
import com.tepth.appmanageservice.tools.ext.ExtToBytes;

public class Controller {
	private static IProtocolEvent _ProtocolEvent;

	public static void setProtocolEvent(IProtocolEvent protocolEvent) {
		_ProtocolEvent = protocolEvent;
	}

	public static void protocol(byte[] data) throws Exception {
		Flag flag = new Flag();
		Header header = new Header();
		Index index = new Index();
		Check check = new Check();
		Flag end = new Flag();

		byte[] body = ExtToBytes.generatePacketsBodyData(data, index, flag, header, check, end);

		if (check.isCheck()) {
			if (header.getIsAES()){
				try {
					body = ExtPro.decrypt(ExtPro.getClient().getAES_KEYS(), body);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			Packets packets = new Packets(flag, header, body, check, end);
			_ProtocolEvent.Invoke(packets);
		} else {
			throw new Exception();
		}
	}

	public static Packets getLoginBytes(byte[] aesKeys) {
		ClientLogin body = new ClientLogin();
		return new Packets(body);
	}

	public static Packets getCommonReply(Packets request, byte status) {
		CommonReply body = new CommonReply();
		body.setReply_id(request.getHeader().getId());
		body.setReplySerialNumber(request.getHeader().getSerialNumber1());
		body.setValue(status);
		return new Packets(body);
	}

	public static Packets getCommonReply(byte status) {
		CommonReply body = new CommonReply();
		body.setReply_id((short) 0);
		body.setReplySerialNumber((short) 0);
		body.setValue(status);
		return new Packets(body);
	}

	// System --------------------------------------------------------------------------------------------

	public static Packets getViewDomainPortReply(Packets packets, String domain, short port) {
		ViewDomainPortReply body = new ViewDomainPortReply();
		body.setDomain(domain);
		body.setPort(port);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	public static Packets getViewHeartbeatFrequencyReply(Packets packets, int value) {
		ViewHeartbeatFrequencyReply body = new ViewHeartbeatFrequencyReply();
		body.setValue(value);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	public static Packets getViewSystemVersionNumberReply(Packets packets, String sytemVersionNumber) {
		ViewSystemVersionNumberReply body = new ViewSystemVersionNumberReply();
		body.setValue(sytemVersionNumber);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	// Aes -----------------------------------------------------------------------------------------------

	public static Packets getAesKey(byte[] password) {
		AesKey body = new AesKey();
		body.setKeys(password);
		return new Packets(body);
	}

	// Ordinary File -------------------------------------------------------------------------------------

	public static Packets getOrdinaryFileRequest(String md5, byte fileType, short packetNo) {
		OrdinaryFileRequest body = new OrdinaryFileRequest();
		body.setMd5(md5);
		body.setFileType(fileType);
		body.setPacketNo(packetNo);
		return new Packets(body);
	}

	public static Packets getViewOrdinaryFileListReply(Packets packets, ViewOrdinaryFileListReply.OrdinaryFile[] ordinaryFileList, byte fileType) {
		ViewOrdinaryFileListReply body = new ViewOrdinaryFileListReply();
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		body.setFileType(fileType);
		body.setFileList(ordinaryFileList);
		return new Packets(body);
	}

	// Sd ------------------------------------------------------------------------------------------------

	public static Packets getViewSdStatusReply(Packets packets, byte value) {
		ViewSdStatusReply body = new ViewSdStatusReply();
		body.setValue(value);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	public static Packets getViewSdSizeReply(Packets packets, int value) {
		ViewSdSizeReply body = new ViewSdSizeReply();
		body.setValue(value);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	public static Packets getViewSdUsedSizeReply(Packets packets, int value) {
		ViewSdUsedSizeReply body = new ViewSdUsedSizeReply();
		body.setValue(value);
		body.setReplySerialNumber(packets.getHeader().getSerialNumber1());
		return new Packets(body);
	}

	// Other ---------------------------------------------------------------------------------------------

	public static Packets getPublicKeyRequest() {
		PublicKeyRequest body = new PublicKeyRequest();
		return new Packets(body);
	}
}